# 支持向量机
import numpy as np
import matplotlib.pyplot as plt


def load_data(input_file):
    X = []
    y = []
    with open(input_file,'r') as f:
        for line in f.readlines():
            data = [float(x) for x in line.split(",")]
            X.append(data[:-1])
            y.append(data[-1])

    X = np.array(X)
    y = np.array(y)

    return X,y


input_file = 'F:/python学习资料/Python-Machine-Learning-Cookbook-master/Chapter03/data_multivar.txt'
X,y = load_data(input_file)
# 把数据分类
class_0 = np.array([X[i] for i in range(len(X)) if y[i] == 0])
class_1 = np.array([X[i] for i in range(len(X)) if y[i] == 1])
# 分好类之后，画出这些数据点
plt.figure()
plt.scatter(class_0[:,0],class_0[:,1],facecolors='black',edgecolors='black',marker='s')
plt.scatter(class_1[:,0],class_1[:,1],facecolors='None',edgecolors='black',marker='s')
plt.title("Input data")
plt.show()

# 划分训练集与测试集
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=5,test_size=0.25)

from sklearn.svm import SVC
# # 下面引入线性核函数来初始化svm对象
# params = {'kernel':'linear'}
# classifier = SVC(**params,gamma='auto')
# # 训练模型
# classifier.fit(X_train,y_train)

from sklearn.metrics import classification_report

# 查看训练集的准确度
# print("********************线性核函数***************************")
# print('\n线性核函数在训练集上的表现')
# print("训练集精度",classifier.score(X_train,y_train))
# target_names = ['Class-'+str(int(i)) for i in set(y)]
# print(classification_report(y_train,classifier.predict(X_train),target_names=target_names))
# print("\n"+'#'*30)
#
# # 查看测试集的准确度
# print('\n线性核函数在测试集上的表现')
# print('训练集精度',classifier.score(X_test,y_test))
# print(classification_report(y_test,classifier.predict(X_test),target_names=target_names))

params = {'kernel':'poly','degree':3}
classifier = SVC(**params,gamma='auto')
classifier.fit(X_train,y_train)
print("#"*30)
print("多项式核函数-训练集\n")
target_names = ['Class-'+str(int(i)) for i in set(y)]
print(f"精度:{round(classifier.score(X_train,y_train),2)}")
print(classification_report(y_train,classifier.predict(X_train),target_names=target_names))

print("#"*30)
print("多项式核函数-测试集\n")
target_names = ['Class-'+str(int(i)) for i in set(y)]
print(f"精度:{round(classifier.score(X_test,y_test),2)}")
print(classification_report(y_test,classifier.predict(X_test),target_names=target_names))


params = {'kernel':'rbf'}
classifier = SVC(**params,gamma='auto')
classifier.fit(X_train,y_train)
print("#"*30)
print("高斯核函数-训练集\n")
target_names = ['Class-'+str(int(i)) for i in set(y)]
print(f"精度:{round(classifier.score(X_train,y_train),2)}")
print(classification_report(y_train,classifier.predict(X_train),target_names=target_names))

print("#"*30)
print("高斯式核函数-测试集\n")
target_names = ['Class-'+str(int(i)) for i in set(y)]
print(f"精度:{round(classifier.score(X_test,y_test),2)}")
print(classification_report(y_test,classifier.predict(X_test),target_names=target_names))